package br.com.nrti.still.eventos;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.Context;
import android.net.ConnectivityManager;

public class Dados {
	public static final String ACAO_DADOS_NAO_ALTERAR = "NAO";
	public static final String ACAO_DADOS_DESLIGAR = "DES";
	public static final String ACAO_DADOS_LIGAR = "LIG";
	public static final String ACAO_DADOS_VOLTAR_ANTERIOR = "ANT";
	
	private static ConnectivityManager conManager;

	public static void inicializar(Context context) {
		conManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
	}

	public static boolean isEnabled() {
		return (conManager.getActiveNetworkInfo() != null && conManager
				.getActiveNetworkInfo().isConnected());
	}

	public static void setAtivo() throws Exception {
		final Class conmanClass = Class
				.forName(conManager.getClass().getName());
		final Field iConnectivityManagerField = conmanClass
				.getDeclaredField("mService");
		iConnectivityManagerField.setAccessible(true);
		final Object iConnectivityManager = iConnectivityManagerField
				.get(conManager);
		final Class iConnectivityManagerClass = Class
				.forName(iConnectivityManager.getClass().getName());
		final Method setMobileDataEnabledMethod = iConnectivityManagerClass
				.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
		setMobileDataEnabledMethod.setAccessible(true);

		setMobileDataEnabledMethod.invoke(iConnectivityManager, true);
	}

	public static void setInativo() throws Exception {
		final Class conmanClass = Class
				.forName(conManager.getClass().getName());
		final Field iConnectivityManagerField = conmanClass
				.getDeclaredField("mService");
		iConnectivityManagerField.setAccessible(true);
		final Object iConnectivityManager = iConnectivityManagerField
				.get(conManager);
		final Class iConnectivityManagerClass = Class
				.forName(iConnectivityManager.getClass().getName());
		final Method setMobileDataEnabledMethod = iConnectivityManagerClass
				.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
		setMobileDataEnabledMethod.setAccessible(true);

		setMobileDataEnabledMethod.invoke(iConnectivityManager, false);
	}
}
